package com.camel.base.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.camel.base.feign.SysCompanyService;
import com.camel.base.model.ZsPstnnumber;
import com.camel.base.service.ZsPstnnumberService;
import com.camel.core.controller.BaseCommonController;
import com.camel.core.entity.IFilter;
import com.camel.core.entity.IParams;
import com.camel.core.entity.Result;
import com.camel.core.exceptions.CustomException;
import com.camel.core.service.BaseCommonService;
import com.camel.core.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.LinkedHashMap;

/**
 * @author camel
 */
@RestController
@RequestMapping("zsPstn")
public class ZsPstnnumberController extends BaseCommonController<ZsPstnnumber> {

    @Autowired
    private SysCompanyService companyService;

    @Autowired
    private ZsPstnnumberService<ZsPstnnumber> service;

    @Override
    public Result index(@RequestBody IParams params) {
        // 设置默认筛选项
        boolean hasDefault = false;
        for (IFilter filter : params.getFilters()) {
            if ("belongUser".equals(filter.getFieldName())) {
                filter.setCompare("=");
                filter.setFieldValue(StpUtil.getLoginIdAsInt());
                hasDefault = true;
                break;
            }
        }
        if (!hasDefault) {
            IFilter iFilter = new IFilter("belongUser", "=", StpUtil.getLoginIdAsInt());
            params.getFilters().add(iFilter);
        }
        return super.index(params);
    }

    @Override
    public boolean deleteAble(Serializable id) {
        ZsPstnnumber detail = getiService().getById(id);
        if (ObjectUtils.isEmpty(detail)) {
            throw new CustomException("删除失败: 外呼号已经删除");
        }
        if (!ObjectUtils.isEmpty(detail.getCompanyId())) {
            throw new CustomException("删除失败: 外呼号已经被分配请回收后重试");
        }
        return true;
    }

    @Override
    public Result save(@RequestBody ZsPstnnumber entity) {
        entity.setBelongUser(StpUtil.getLoginIdAsInt());
        return super.save(entity);
    }

    @PostMapping("/auth")
    public Result auth(@RequestBody ZsPstnnumber entity) {
        if(ObjectUtils.isEmpty(entity.getCompanyId())) {
            throw new CustomException("未选择授权公司");
        }
        Result details = companyService.details(entity.getCompanyId());
        if(!ObjectUtils.isEmpty(details.JSONObject())) {
            entity.setCompanyName(details.JSONObject().getString("name"));
        }
        service.updateById(entity);
        return ResultUtil.success("授权成功");
    }

    @Override
    public BaseCommonService<ZsPstnnumber> getiService() {
        return service;
    }

    @Override
    public String getMouduleName() {
        return "外线号码";
    }
}
